﻿using System;
using System.Runtime.Serialization;
using XcoAppSpaces.Contracts.Security;
using XcoAppSpaces.Contracts.WorkerResolve;
using XcoAppSpaces.Core.Ports;

namespace XcoAppSpaces.Serialization.Binary
{
	class ResolveRequestSerializationSurrogate : ISerializationSurrogate
	{
		public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
		{
			var msg = (ResolveRequest)obj;
			info.AddValue("ClientCredentials", msg.ClientCredentials);
			info.AddValue("ID", msg.ID);
			info.AddValue("ResponsePort", msg.ResponsePort);
			info.AddValue("WorkerAddress", msg.WorkerAddress);
			info.AddValue("WorkerIdentifier", msg.WorkerIdentifier);
		}

		public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
		{
			obj = new ResolveRequest
				{
					ClientCredentials = (IXcoClientCredentials) info.GetValue("ClientCredentials", typeof (IXcoClientCredentials)),
					ID = (Guid) info.GetValue("ID", typeof (Guid)),
					ResponsePort = (Port<ResolveResponse>) info.GetValue("ResponsePort", typeof (Port<ResolveResponse>)),
					WorkerAddress = (string) info.GetValue("WorkerAddress", typeof (string)),
					WorkerIdentifier = (string) info.GetValue("WorkerIdentifier", typeof (string)),
				};
			return obj;
		}
	}
}